<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Announcement extends CI_Controller {

    public $session_data;

    public function __construct()
    {
        parent::__construct();
        //Not Completed
        $this->load->model('info_model');
        $this->load->model('announcement_model');
        $this->load->helper("url");
        $this->load->library("pagination");

        $this->session_data = $this->session->userdata('logged_in');
    }

    public function view()
    {

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $data['owner_id'] = $this->session_data['user_id'];

        $this->load->helper('form');
        $this->load->library("pagination");
        $data['title'] = "Announcement";

        $session_data = $this->session->userdata('logged_in');


        $config["base_url"] = base_url()."index.php/announcement/view";
        $config["total_rows"] = $this->announcement_model->get_user_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

        $result = $this->info_model->check_acc($session_data['user_id']);
        if($session_data['type_no'] != 1)
            $data['annc'] = $this->announcement_model->announcement_to_all($config["per_page"], $page, $result->user_id);
        else
            $data['annc'] = "";

        //echo var_dump($data['annc']);

        if(empty($data['annc'])){

            //echo "Event activated";

            $data['annc'] = $this->announcement_model->announcement_by_college($config["per_page"], $page, $session_data['coll_id']);

        }

        //echo var_dump($data['annc']);
        //echo var_dump($session_data);

        $data["links"] = $this->pagination->create_links();
        $data['college'] = $this->announcement_model->get_college_list();
        $data['typeh'] = $session_data['type_no'];
        $data['type'] = $data['typeh'];
        $data['college_user'] = $session_data['coll_id'];

        $this->load->view('template/header', $data);
        $this->load->view('pages/home',$data);
        $this->load->view('template/footer');

    }

    public function add_announcement()
    {
        //THERE IS NO FUNCTION THAT DETECTS WHETHER THE INPUT USERNAME IS DUPLICATE OR NOT

        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
        $this->form_validation->set_rules('desc', 'Description', 'trim|required|xss_clean');
        //$this->form_validation->set_rules('college', 'College', 'trim|xss_clean');

        if ($this->form_validation->run() === FALSE)
        {

            redirect('announcement/view','refresh');

        }

        else
        {
            $this->load->helper('date');
            $college = $this->input->post('college');

            $datestring = "%m,%d,%Y,%h,%i,%s,%a";
            $time = time();
            $date = mdate($datestring, $time);


            //echo var_dump($this->session_data);
            //echo var_dump($this->session_data['user_id']);
            //echo var_dump($this->input->post('desc'));

            if($college == false){
                $college_list = $this->announcement_model->get_college_list();

                foreach($college_list as $item){

                    echo var_dump($item) . "<br />";

                    $new = array(
                        "user_id" => $this->session_data['user_id'],
                        "title" => $this->input->post('title'),
                        "date" => $date,
                        "content" => $this->input->post('desc'),
                        "coll_id" => $item['coll_id']
                    );

                    $this->announcement_model->add_announcement($new);

                }

            }
            else{

                foreach($college as $item){

                    $new = array(
                        "user_id" => $this->session_data['user_id'],
                        "title" => $this->input->post('title'),
                        "date" => mdate($datestring, $time),
                        "content" => $this->input->post('desc'),
                        "coll_id" => $item
                    );

                    $this->announcement_model->add_announcement($new);

                }

            }

            //echo $date;

            redirect('announcement/view','refresh');

        }

    }

    public function delete($id){

        //This function should check if the user is one level higher than the chosen user so that they can delete them.
        //Can't yet delete the files but can delete the photos

        if (!$this->session->userdata('logged_in'))
            redirect('manage_department/manage_dept','refresh');

        $this->announcement_model->delete($id);

        redirect('announcement/view','refresh');

    }

    function check_user ()
    {

        $username = $this->input->post('user');
        $result = $this->manage_department_model->check_acc($username);

        $data['owner_id'] = $this->session_data['user_id'];
        var_dump($data['owner_id']);

        if($result)
        {

            $this->load->view('template/header', $data);
            $this->load->view('pages/manage_department', $data);
            $this->load->view('template/footer');
            return TRUE;

        }
        else
        {
            //$this->form_validation->set_message('check_user', "Invalid Username or Password");
            return false;
        }

    }

    public function logout()
    {

        $this->session->unset_userdata('logged_in');
        redirect('welcome/home', 'refresh');

    }

}

/* End of file manage_account.php */
/* Location: ./application/controllers/manage_account.php */